let mongoose = require('mongoose');
const conn = mongoose.createConnection('mongodb://localhost:27017/db201808',{ useNewUrlParser: true });
let UserSchema = new mongoose.Schema({
    username:{type:String,required:true},
    password:String,
    age:Number,
    createAt:{type:Date,default:Date.now}
},{collection:'user'});
// 集合的名字是来自于模型的名字，模型名》小写》复数
let User = conn.model('User',UserSchema);
//插入文档
(async function(){
    let user = await  User.create({username:'lisi',age:10,password:'123456'});
    console.log(user);
    //更新默认是更新匹配到的第一条数据 如果要更新多条 multi:true
    //let updateResult = await User.update({username:'lisi'},{age:11});
    let updateResult1 = await User.updateOne({username:'lisi'},{age:11});
    let updateResult2 = await User.updateMany({username:'lisi'},{age:11});
    let queryResult1 = await User.find({username:'lisi'},{username:1,age:1});
    let queryResult2 = await User.findOne({username:'lisi',password:'123456'});
    let queryResult3 =await User.findById('5c938874a973313c04232ccc');
    let queryResult4 =await User.find({age:{$exist:true},age:{$gt:1,$lte:11},username:{$in:['lisi','zhangsan']}});
    console.log(queryResult4);
})()